const { Model, DataTypes } = require('sequelize');
const sequelize = require('../db/db');
const User = require('./User');

class UserCheckin extends Model { }

UserCheckin.init(
  {
    user_id: {
      type: DataTypes.CHAR(18),
      primaryKey: true,
      allowNull: false,
      comment: '用户ID',
      references: {
        model: User,
        key: 'user_id'
      }
    },
    checkin_date: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: '签到日期'
    },
    checkin_time: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '签到完整时间'
    },
    streak_days: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '连续签到天数'
    },
    max_streak: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '最大连续签到天数'
    },
    checkin_history: {
      type: DataTypes.JSON,
      allowNull: false,
      defaultValue: [],
      comment: '签到历史记录'
    }
  },
  {
    sequelize,
    modelName: 'UserCheckin',
    tableName: 'user_checkin',
    timestamps: false,
    comment: '用户签到表'
  }
);

// 建立与User表的关联
UserCheckin.belongsTo(User, {
  foreignKey: 'user_id',
  targetKey: 'user_id'
});

module.exports = UserCheckin;